package com.software.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.software.base.ResultInfo;
import com.software.bean.CustomerInfoBean;
import com.software.dto.UpdateAvatarImageDto;
import com.software.dto.UpdatePersonalDataDto;
import com.software.service.CustomerInfoService;
import com.software.utils.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wyj
 * @since 2024-03-19
 */
@Controller
@RequestMapping("/customerInfo")
public class CustomerInfoController {

    @Autowired
    private CustomerInfoService customerInfoService;

    /**
     * @description:
     * @author: wyj
     * @date: 2024/3/20 18:29
     * @param:
     * @return:后台分页
     */
    @GetMapping("/api/getUserList")
    @ResponseBody
    public ResultInfo getUserList(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        IPage<CustomerInfoBean> iPage = customerInfoService.getUserListForPage(pageNum, pageSize);
        return ResultInfo.success((int) iPage.getTotal(), iPage.getRecords());
    }

    /**
     * @description:
     * @author: wyj
     * @date: 2024/3/20 22:34
     * @param:
     * @return:前端获取个人信息页面
     */
    @GetMapping("/front/getPersonalData")
    @ResponseBody
    public ResultInfo getPersonalData(@RequestParam Integer id) {
        CustomerInfoBean customerInfoBean = customerInfoService.getPersonalData(id);
        AssertUtil.isTrue(customerInfoBean == null, "返回值异常2");
        return ResultInfo.success(customerInfoBean);
    }

    /**
     * @description:
     * @author: wyj
     * @date: 2024/3/21 23:10
     * @param:
     * @return:更新个人信息
     */
    @PostMapping("/front/updatePersonalData")
    @ResponseBody
    public ResultInfo updatePersonalData(@RequestBody UpdatePersonalDataDto updatePersonalDataDto) {
        CustomerInfoBean
                result = customerInfoService.updatePersonalData(updatePersonalDataDto);
        AssertUtil.isTrue(result == null, "更新失败");
        return ResultInfo.success(result);
    }

    /**
     * @description:更新头像
     * @author: wyj
     * @date: 2024/3/29 23:50
     * @param:
     * @return:
     */
    @PostMapping("/front/updateCustomerAvatarImage")
    @ResponseBody
    public ResultInfo updateCustomerAvatarImage(UpdateAvatarImageDto avatarImageDto) {
        System.out.println(avatarImageDto);
        Integer result = customerInfoService.updateAvatarImage(avatarImageDto);
        AssertUtil.isTrue(result != 1, "更新失败");
        CustomerInfoBean personalData = customerInfoService.getPersonalData(avatarImageDto.getCustomerId());
        AssertUtil.isTrue(personalData == null, "返回值异常");
        return ResultInfo.success(personalData);
    }

    /**
     * @description:禁用账户
     * @author: wyj
     * @date: 2024/4/3 0:17
     * @param:customerId
     * @return:
     */
    @PutMapping("/api/disableUser/{id}")
    @ResponseBody
    public ResultInfo disableUser(@PathVariable Integer id) {
        Integer result = customerInfoService.disableUser(id);
        AssertUtil.isTrue(result != 1, "禁用失败");
        return ResultInfo.success();
    }

    /**
     * @description:启用账户
     * @author: wyj
     * @date: 2024/4/3 0:38
     * @param:
     * @return:
     */
    @PutMapping("/api/enableUser/{id}")
    @ResponseBody
    public ResultInfo enableUser(@PathVariable Integer id) {
        Integer result = customerInfoService.enableUser(id);
        AssertUtil.isTrue(result != 1, "启用失败");
        return ResultInfo.success();
    }

}
